//
//  MayLikeTableViewCell.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/4.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "MayLikeTableViewCell.h"
#import "MayLikeView.h"
#import "MayLikeModel.h"

@implementation MayLikeTableViewCell

+ (MayLikeTableViewCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"MayLikeTableViewCell";
    MayLikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[MayLikeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}

- (void)setMayLikeArray:(NSMutableArray *)mayLikeArray {
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(156)) / 2, ScreenX375(22), ScreenX375(156), 1)];
    line.backgroundColor = LineColor;
    [self addSubview:line];
    
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(76)) / 2, ScreenX375(14), ScreenX375(76), ScreenX375(16))];
    noteLabel.backgroundColor = [UIColor whiteColor];
    noteLabel.text = @"猜你喜欢";
    noteLabel.textColor = REXADECIMALCOLOR(@"#999999");
    noteLabel.textAlignment = NSTextAlignmentCenter;
    noteLabel.font = Kfont(14);
    [self addSubview:noteLabel];
    _mayLikeArray = mayLikeArray;
    CGFloat width = (SCREEN_WIDTH - ScreenX375(39)) / 2;
    for (int i = 0; i < mayLikeArray.count; i++) {
        MayLikeView *view = [[MayLikeView alloc] initWithFrame:CGRectMake(i % 2 * (width + ScreenX375(13)) + ScreenX375(13), i / 2 * ScreenX375(175) + ScreenX375(39), width, ScreenX375(165))];
        MayLikeModel *model = [_mayLikeArray objectAtIndex:i];
        [view initWithModel:model];
        [self addSubview:view];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i % 2 * (width + ScreenX375(13)) + ScreenX375(13), i / 2 * ScreenX375(175) + ScreenX375(39), width, ScreenX375(165))];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
}

- (void)click:(UIButton *)button {
    if (_indexClick) {
        _indexClick(button.tag - 100);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
